package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.SpecificArea;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.mapper.SpecificAreaMapper;
import cn.wolfcode.trip.base.mapper.TravelCommendMapper;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper contentMapper;
    @Autowired
    private TravelCommendMapper travelCommendMapper;
    @Autowired
    private SpecificAreaMapper specificAreaMapper;

    public void changeState(Long id, Integer state) {
        //判断操作的类型
        if (state == Travel.STATE_REFUSE) {
            //拒绝操作
            travelMapper.updateState(id, state, null);
        } else if (state == Travel.STATE_RELEASE) {
            //发布操作
            //设置发布的时间
            travelMapper.updateState(id, state, new Date());
        }
    }

    @Override
    public List<Travel> selectForStrategyId(Long strategyId) {
        Long placeId = specificAreaMapper.get(strategyId);
        return travelMapper.selectForPlaceId(placeId);
    }

    public String getContent(Long id) {
        return contentMapper.selectByPrimaryKey(id).getContent();
    }

    public Travel get(Long id) {
        //1:根据id查询到游记对象
        Travel travel = travelMapper.selectByPrimaryKey(id);
        //2:根据游记的id查询内容对象,设置到游记中
        TravelContent travelContent = contentMapper.selectByPrimaryKey(id);
        travel.setTravelContent(travelContent);
        return travel;
    }

    public PageInfo<Travel> queryTravel(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    public void saveOrUpdate(Travel entity) {
        if (entity.getId() == null) {
            //1:自动填充创建时间/最后更新时间
            Date d = new Date();
            entity.setCreateTime(d);
            entity.setLastUpdateTime(d);
            travelMapper.insert(entity);
            //2:同步保存游记的内容
            TravelContent travelContent = entity.getTravelContent();
            //3:主键关联
            travelContent.setId(entity.getId());
            contentMapper.insert(travelContent);
        } else {
            //1:设置最后更新时间
            entity.setLastUpdateTime(new Date());
            //2:更新游记
            travelMapper.updateByPrimaryKey(entity);
            //3:设置主键关联
            TravelContent travelContent = entity.getTravelContent();
            travelContent.setId(entity.getId());
            //4:更新游记内容
            contentMapper.updateByPrimaryKey(travelContent);
        }
    }
}
